import fs from 'fs'
import path from 'path'


export function allFiles(contorllers) {

    let allFiles = fs.readdirSync(contorllers).filter(item => item.endsWith('.js')).map(item => item = `file:///${path.resolve()}/${contorllers}/${item}`)
    return allFiles
}

export function registryRouter(router, files) {
    files.forEach(async file => {
        let { obj } = await import(file)
        for (let key in obj) {
            let [keyMethod, keyPath] = key.split(' ')
            let keyFn = obj[key]
            // console.log(keyMethod)
            // console.log(keyPath)
            // console.log(keyFn)

            if (keyMethod === 'get') {
                router.get(keyPath, keyFn)
            }
            if (keyMethod === 'post') {
                router.post(keyPath, keyFn)
            }
            if (keyMethod === 'put') {
                router.put(keyPath, keyFn)
            }
            if (keyMethod === 'delete') {
                router.delete(keyPath, keyFn)
            }
        }

    })

}


